
import datetime
from sensor_info_table_methods import SensorInfoTableMethods
from xlsx_methods import XlsxMethods
from command_parse import CommandParse
from settings import SEND_INTERVAL
class IndexData:
    def __init__(self):

        with XlsxMethods() as xlsx_methods:
            self.index_data = xlsx_methods.get_info()

            '''\
            self.index_data output: {
            'master_station_mac': '00000000', 
            'group_data_list': [
                {'group_id': 1, 'bracket_number': 7, 'station_mac': '0100', 'sensor_list': [{'sensor_type': 'A', 'sensor_mac': '010A', 'sensor_type_name': '倾角'}, {'sensor_type': 'B', 'sensor_mac': '010B', 'sensor_type_name': '倾角'}, {'sensor_type': 'C', 'sensor_mac': '010C', 'sensor_type_name': '倾角'}, {'sensor_type': 'D', 'sensor_mac': '010D', 'sensor_type_name': '压力'}, {'sensor_type': 'E', 'sensor_mac': '010E', 'sensor_type_name': '压力'}]}, 
                {'group_id': 2, 'bracket_number': 12, 'station_mac': '0200', 'sensor_list': [{'sensor_type': 'A', 'sensor_mac': '020A', 'sensor_type_name': '倾角'}, {'sensor_type': 'B', 'sensor_mac': '020B', 'sensor_type_name': '倾角'}, {'sensor_type': 'C', 'sensor_mac': '020C', 'sensor_type_name': '倾角'}, {'sensor_type': 'D', 'sensor_mac': '020D', 'sensor_type_name': '压力'}, {'sensor_type': 'E', 'sensor_mac': '020E', 'sensor_type_name': '压力'}]}
            ]
            }'''
    
    def get_index_data(self, start_time=None, end_time=None):

        online_count = 0
        offline_count = 0

        for index_data in self.index_data["group_data_list"]:

            for sensor_data in index_data["sensor_list"]:

                print(sensor_data)

                with SensorInfoTableMethods() as sensor_info_table_methods:

                    # print(sensor_data)
                    # print(sensor_data["sensor_mac"])
                    
                    online_status = sensor_info_table_methods.now_online_status(sensor_address=sensor_data["sensor_mac"].lower())

                    if online_status == "online":
                        online_count += 1
                    else:
                        offline_count += 1
                    # print(online_status)
                    sensor_info = sensor_info_table_methods.get_sensor_info(sensor_address=sensor_data["sensor_mac"].lower())
                    # print(sensor_info)

                    # 默认为最近4小时内断交次数 时间参数格式2024-08-07 10:31:35
                    interrupted_times = sensor_info_table_methods.interrupted_times(sensor_address=sensor_data["sensor_mac"].lower(),
                                                                                    start_time=start_time,
                                                                                    end_time=end_time, 
                                                                                    )
                update_dict = {
                    "online_status": online_status, 
                    "update_time": None, 
                    "row_data": None, 
                    "parse_result": None,
                    "total_upload_times": None,
                    "interrupted_times": None,
                    "should_upload_times": None,
                }

                if sensor_info:
                    # print(online_status)
                    # print(sensor_info)
                    '''sensor_info output: (75549, '258', '倾角', 'e51b0000000102756303071400000000811300795000010100d826', '2024-08-07 10:31:35', None)'''
                
                    update_dict["online_status"] = online_status
                    # update_dict["update_time"] = sensor_info[4] # 时间格式2024-08-07 10:31:35 变成东八区时间
                    update_dict["update_time"] = (datetime.datetime.strptime(sensor_info[4], '%Y-%m-%d %H:%M:%S') + datetime.timedelta(hours=8)).strftime('%Y-%m-%d %H:%M:%S') # 时间格式2024-08-07 10:31:35 变成东八区时间
                    update_dict["row_data"] = sensor_info[3]
                    try:
                        update_dict["parse_result"] = CommandParse(command=sensor_info[3]).parse_result

                    except Exception as e:
                        print(e)
                        update_dict["parse_result"] = "解析失败"
                    
                    update_dict["interrupted_times"] = interrupted_times
                sensor_data.update(update_dict)
        self.index_data["online_count"] = online_count
        self.index_data["offline_count"] = offline_count
        self.index_data["send_interval"] = SEND_INTERVAL, 

        return self.index_data
if __name__ == '__main__':
    index_data = IndexData()
    index_data.get_index_data()


